// 消息类型定义
interface MessageData {
  type: "success" | "error" | "warning" | "info";
  content: string;
  duration?: number;
}

// 全局消息管理器
class MessageManager {
  private listeners: Array<(message: MessageData) => void> = [];

  // 注册监听器
  subscribe(listener: (message: MessageData) => void) {
    this.listeners.push(listener);
    return () => {
      this.listeners = this.listeners.filter((l) => l !== listener);
    };
  }

  // 发送消息
  private emit(message: MessageData) {
    this.listeners.forEach((listener) => listener(message));
  }

  success(content: string, duration?: number) {
    this.emit({ type: "success", content, duration });
  }

  error(content: string, duration?: number) {
    this.emit({ type: "error", content, duration });
  }

  warning(content: string, duration?: number) {
    this.emit({ type: "warning", content, duration });
  }

  info(content: string, duration?: number) {
    this.emit({ type: "info", content, duration });
  }
}

export const message = new MessageManager();
